package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Shop;
import cn.itsource.pethome.org.query.ShopQuery;
import cn.itsource.pethome.org.service.IShopService;
import cn.itsource.pethome.utils.AjaxResult;
import cn.itsource.pethome.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){
                shopService.save(shop);
            }else{
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMessage("保存失败"+e.getMessage());
        }
    }

    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        shopService.delete(id);
        return AjaxResult.me();
    }

    @GetMapping(value = "/{id}")
    public Shop selectById(@PathVariable("id") Long id){
        return shopService.loadById(id);
    }

    @GetMapping
    public List<Shop> selectAll(){
        return shopService.loadAll();
    }

    @PostMapping
    public PageResult<Shop> selectByPage(@RequestBody ShopQuery query){
        return shopService.selectByPage(query);
    }

    @PatchMapping
    public AjaxResult deletes(@RequestBody Long[] ids){
        shopService.deletes(ids);
        return AjaxResult.me();
    }

    @PostMapping("/settledIn")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("入驻失败"+e.getMessage());
        }
    }

    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody Shop shop){
        try {
            shopService.update(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络延迟" + e.getMessage());
        }
    }
}
